package com.weiaibenpao.demo.chislim.ui.activity.moment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.Message;
import com.weiaibenpao.demo.chislim.bean.MessageListBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.MessageListPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.MessageListAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.MessageListView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MessageListActivity extends BaseActivity<MessageListView,MessageListPresenter> implements MessageListView,BaseQuickAdapter.OnItemClickListener,BaseQuickAdapter.RequestLoadMoreListener{
    public static final int REQUEST_MESSAGE_LIST = 111;
    private int mNextPage =1;
    private int mUserId ;
    private MessageListAdapter mAdapter;
    @BindView(R.id.mid_text)
    TextView mTvTitle;
    @BindView(R.id.rv_list)
    RecyclerView mMessageList;


    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_message_list;
    }

    @Override
    public MessageListPresenter initPresenter() {
        return new MessageListPresenter();
    }

    @Override
    public void init(Bundle savedInstanceState) {
        mUserId = UserPreferences.getInstance().getInt(UserPreferences.USER_ID);
        initView();
        mPresenter.getMessageList(mUserId,mNextPage);
        //可以设置setEmptyView--ErrorView   NoDataView
    }

    private void initView() {
        mAdapter = new MessageListAdapter();
        mAdapter.setOnLoadMoreListener(this,mMessageList);
        mAdapter.setOnItemClickListener(this);
        mMessageList.setLayoutManager(new LinearLayoutManager(this));
        mMessageList.setAdapter(mAdapter);
    }

    public static void startSelf(Context context, Fragment fragment){
        Intent intent  = new Intent(context,MessageListActivity.class);
        fragment.startActivityForResult(intent,REQUEST_MESSAGE_LIST);
    }

    @OnClick({R.id.right_text,R.id.left_image})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.left_image:
                finish();
                break;
            case R.id.right_text:
                mPresenter.clearAllMessage(mUserId);
                break;
        }
    }

    @Override
    public void onGetMessageListSuccess(MessageListBean messageListBean) {
        if(messageListBean.getCountRealInfo()>0){
            mTvTitle.setText(getString(R.string.title_message_list)+"("+messageListBean.getCountRealInfo()+")");
        }else {
            mTvTitle.setText(getString(R.string.title_message_list));
        }
        setData(messageListBean.getList());
        setResult(RESULT_OK);//服务端做的处理是：只要请求消息列表成功就把未读数清零
        mNextPage++;
        Logger.e("onGetMessageListSuccess--");
    }

    private void setData(List<Message> data) {
        final int size = data == null ? 0 : data.size();
        if (size > 0) {
            mAdapter.addData(data);
        }
        if (size < Constant.DEFAULT_PAGE_SIZE) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onGetMessageListFailed(String errMsg) {
        Logger.e("onGetMessageListFailed--"+errMsg);
        mAdapter.loadMoreFail();
    }

    @Override
    public void onClearMessageSuccess() {
        mAdapter.getData().clear();
        mAdapter.notifyDataSetChanged();
        Logger.e("onClearMessageSuccess--");
    }

    @Override
    public void onClearMessageFailed(String errMsg) {
        Logger.e("onClearMessageFailed--"+errMsg);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Message message = mAdapter.getData().get(position);
        MomentDetailsActivity.startSelf(this,message.getHumorId(),message.getUserId());
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.getMessageList(mUserId,mNextPage);
    }
}
